﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IVisual._SC.AutoDiscovery;

namespace IVisual._SC
{
    /// <summary>
    /// Allgemeine Form für die Verbdinung mit AD
    /// </summary>
    public partial class frm_Connect : Form
    {
        private BackgroundWorker _AutoDiscovery_cli;
        private cls_AutoDiscovery_Client svcAutoDiscoveryClient;

        private bool _AD = true;

        public System.Net.IPAddress IP
        {
            get
            {
                return cls_tools.try_resolve(this._tb_ip.Text);
            }
            set
            {
                this._tb_ip.Text = value.ToString();
            }
        }
        
        public long Port
        {
            get
            {
                return long.Parse(this._tb_port.Text);
            }
            set
            {
                this._tb_port.Text = value.ToString();
            }
        }

        public frm_Connect(bool AD = true)
        {
            InitializeComponent();

            this._AD = AD;
        }

        /// <summary>
        /// Startet den AD-Client
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_Connect_Load(object sender, EventArgs e)
        {
            if (this._AD)
            {
                _AutoDiscovery_cli = new BackgroundWorker();
                _AutoDiscovery_cli.WorkerReportsProgress = true;
                _AutoDiscovery_cli.WorkerSupportsCancellation = true;

                svcAutoDiscoveryClient = new cls_AutoDiscovery_Client(ref _AutoDiscovery_cli);
                _AutoDiscovery_cli.DoWork += new DoWorkEventHandler(svcAutoDiscoveryClient.Start);
                //_AutoDiscovery_cli.ProgressChanged += new ProgressChangedEventHandler(logWorkers_ProgressChanged);
                _AutoDiscovery_cli.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.AutoDiscovery_cli_RunWorkerCompleted);
                _AutoDiscovery_cli.RunWorkerAsync();
            }
        }

        /// <summary>
        /// AD abgeschlossen.
        /// Wenn erfolgreich, wird das ergebnis in de TBs gesetzt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoDiscovery_cli_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this._pb_autodisc.Visible = false;

            if((bool)e.Result)
            {
                this._tb_ip.Text = cls_tools.try_reverse_resolve(svcAutoDiscoveryClient.ServerAddress);
                this._tb_port.Text = svcAutoDiscoveryClient.ServerPort.ToString();
            }
        }

        /// <summary>
        /// Schließt den Dialog, mit ok, wenn die TBs ausgefüllt sind
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _cmd_verbinden_Click(object sender, EventArgs e)
        {
            if(this._tb_port.Text != "" && this._tb_ip.Text != "")
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        /// <summary>
        /// Schließt den Dalog, mit Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _cmd_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
